﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Pre-Dies Actions Dialog
	This dialog will ask player to Auction property stating Minimum offered Price
*/

package monopoly.GameController.Dialogs {

	import monopoly.GameController.Dialogs.IDialog;
	import monopoly.GameController.Dialogs.Templates.*;
	import monopoly.Player.Player;
	import monopoly.Field.Field;
	import monopoly.IFS.GLOBALS;
	import monopoly.Localization.*;
	import fl.controls.*;
	import fl.containers.ScrollPane;
	import flash.text.TextFieldAutoSize;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	
	public class AuctionDialog extends ScrollPane implements IDialog
		{
		public static const STATUS_OFFER:Boolean	= false;
		public static const STATUS_AUCTION:Boolean	= true;
		
		protected var _field:Field;
		protected var _data:int = 0;
		protected var _status:Boolean = STATUS_AUCTION;
		protected var _myStrings:ILocalLanguage	= LocalLanguage.getInstance();
		
		protected var _priceLabel:Label		= new Label();
		protected var _inputText:TextInput	= new TextInput();
		protected var _OKButton:Button		= new Button();
		protected var _PassButton:Button;
		protected var VerticalPadding:Number = GLOBALS.VPADDING + 5;
		
		
		public function AuctionDialog(field:Field, Status:Boolean)
			{
			_field = field;
			_status = Status;
			
			setupComponents();
			}
		
		// for IDilaog compatilibitly only
		public function get chosenAction():uint
			{
			return 0;
			}
		public function get Data():int
			{
			return _data;
			}
		public function get field():Field
			{
			return _field;
			}
		public function get Status():Boolean
			{
			return _status;
			}
		public function set Data(sum:int):void
			{
			_data = sum;
			_priceLabel.text	= _myStrings.DlgAucLabelNOffer(_field.PropertyName, _data);
			_inputText.text		= String(sum);
			}
		
		protected function setupComponents():void
			{
			this.visible = false;
			
			with (this)
				{
				setSize(180, 200);
				x = 0;
				y = 0;
				alpha = 0.9;
				//opaqueBackground = GLOBALS.DEFAULT_DIALOG_COLOR;
				}
			
			with (_priceLabel)
				{
				move(5, 5);
				text = _myStrings.DlgAucLabelMOffer(_field.PropertyName, _field.Cost);
				autoSize = TextFieldAutoSize.LEFT;
				}
			addChild(_priceLabel);
			
			// price text
			_inputText.maxChars 			= 8;
			_inputText.textField.background = true;
			_inputText.textField.border	   	= true;
			_inputText.text 				= String(_field.Cost);
			_inputText.restrict				= "0123456789";
			_inputText.drawFocus(true);
			_inputText.setSize(this.width * 0.8, this.height*0.1);
			_inputText.move(5, _priceLabel.y + _priceLabel.height + VerticalPadding);
			addChild(_inputText);
			
			_OKButton.addEventListener(MouseEvent.CLICK, buttonClick);
			_OKButton.move(5, _inputText.y + _inputText.height + VerticalPadding);
			_OKButton.label = "OK";
			addChild(_OKButton);
			
			if (STATUS_OFFER == _status)
				{
				_PassButton = new Button();
				_PassButton.addEventListener(MouseEvent.CLICK, buttonClick);
				_PassButton.move(5, _OKButton.y + _OKButton.height + VerticalPadding);
				_PassButton.label = "OK";
				addChild(_PassButton);
				}

			this.visible = true;
			}
		
		protected function buttonClick(e:MouseEvent):void
			{
			switch(e.target) 
				{
                case _OKButton:
					_data = int(_inputText.text);
					break;
				case _PassButton:
					_data = 0;
				}
				
			/* dialog done */	
			
			this.removeEventListener(MouseEvent.CLICK, buttonClick);
			with (this)
				{
				removeChild(_priceLabel);
				removeChild(_inputText);
				removeChild(_OKButton);
				if (null != _PassButton)
					removeChild(_PassButton);
				}
				
			dispatchEvent(new Event(Event.COMPLETE));
			}
		}
}